package net.lzzy.practices.handlers;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

/**
 * @author m
 */ /*
* 相当于
*   ControllerAdviece+RequestBody, 以json的形式返回
*  加上annotations = Rest Controller。class
*
* */
@RestControllerAdvice(annotations = RestController.class)
//拦截带有RestController注解class
public class ApiExceptionHandler {
    public  static  class  ExceptinonWrapper{
//        描述
        private final   String desc;
//        消息
        private final String message;

        private final StackTraceElement[] elements;


        public ExceptinonWrapper(String desc, String message, StackTraceElement[] elements) {
            this.desc = desc;
            this.message = message;
            this.elements = elements;
        }
    }
@ExceptionHandler(Exception.class)
//所有异常都通过这个类处理
    public  ExceptinonWrapper handler(WebRequest request,Exception e){
        return  new ExceptinonWrapper(request.getDescription(true),e.getMessage(),e.getStackTrace());
    }




}
